/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DAL.UserDAL;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.JAXBUtil;
import xmlobject.DTO.Users.User;
import xmlobject.DTO.Users.Users;

/**
 *
 * @author minhsong
 */
public class UserController extends HttpServlet {
    private UserDAL service = new UserDAL();
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            String action = request.getParameter("Action");
            if(action.equals("Register"))
            {

                String birthstr = request.getParameter("BirthDate");
                Date bithdate = new Date(Date.parse(birthstr));
                User newuser = new User();
                newuser.setUsername(request.getParameter("Username"));
                newuser.setPassword(request.getParameter("Password"));
                newuser.setFullName(request.getParameter("Fullname"));
                newuser.setPhoneNumber(request.getParameter("PhoneNumber"));
                newuser.setAddress(request.getParameter("Address"));
                newuser.setRoleID(1);
                newuser.setBirthDate(bithdate);

                service.Create(newuser);

                RequestDispatcher rd = request.getRequestDispatcher("/");
                rd.forward(request, response);
            }else if(action.equals("Login")){
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                String Username = request.getParameter("Username");
                String Password = request.getParameter("Password");
                User user = service.Loging(Username, Password);
                if(user!=null){
                    HttpSession session = request.getSession();
                    session.setAttribute("Username", user.getUsername());
                    RequestDispatcher rd = request.getRequestDispatcher("/");
                    rd.forward(request, response);
                }else
                {
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                }
                out.close();
            }else if(action.equals("ChangePass")){
                
            }else if(action.equals("GetInfo")){
                response.setContentType("text/xml");
                HttpSession session = request.getSession();
                String userName=session.getAttribute("Username").toString();
                UserDAL userDAL=new UserDAL();
                User user=userDAL.getUserById(userName);
                Users users= new Users();
                users.getUser().add(user);
                JAXBUtil.marshal(response.getOutputStream(), users);
            }else if(action.equals("Logout")){
                HttpSession session = request.getSession();
                session.setAttribute("Username", null);
                RequestDispatcher rd = request.getRequestDispatcher("/");
                rd.forward(request, response);
            }
        } finally { 
            
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
